#include<iostream>
#include<sys/types.h>
#include<sys/stat.h>
#include<string>
#include<fcntl.h>
#include<unistd.h>
#include"comm.hpp"
int main()
{
    umask(0);
    int n = mkfifo(FIFO_FILE,0666);//成功返回0,失败返回-1
    if(n != 0)
    {
        std::cerr << "mkdir fifo error" << std::endl;//打印错误信息
        return 1;
    }
    //管道文件创建能成功
    std::cout << "mkdir fifo susser" << std::endl;
    int fd = open(FIFO_FILE,O_RDONLY);//以只读的方式打开
    if(fd < 0)
    {
        //打开文件失败
        std::cerr << "open fifo error" << std::endl;//打印错误信息
        return 2;
    }
    char buffer[1024];
    while(true)
    {
        int number = read(fd,buffer,sizeof(buffer) - 1);//返回实际读取的大小,失败返回-1
        if(number > 0)
        {
            //读取成功
            buffer[number] = 0;
            std::cout << "client say# " << buffer << std::endl;
        }
        else if(number == 0)
        {
            std::cout << "client quit?" << number << std::endl;
            break;
        }
        else
        {
            std::cerr << "read error" << std::endl;
            break;
        }
    }
    close(fd);

    int n1 = unlink(FIFO_FILE);
    if(n1 == 0)
    {
        std::cout << "remover fifo success" << std::endl;
    }
    else
    {
        std::cout << "remover fifo error" << std::endl;
    }
    return 0;
}